package com.fitness.fitness.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.fitness.convertor.VenueConvertor;
import com.fitness.fitness.modal.resource.dto.VenueDto;
import com.fitness.fitness.modal.resource.entity.Venue;
import com.fitness.fitness.modal.resource.params.VenuePageQueryParams;
import com.fitness.fitness.modal.resource.vo.VenueVO;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.IVenueService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/venue")
public class VenueController {

    private final IVenueService venueService;
    private final VenueConvertor venueConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<VenueVO>> page(VenuePageQueryParams queryParams) {
        Page<VenueVO> result = venueService.getVenuePage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param venueDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody VenueDto venueDto) {
        String sn = venueDto.getSn();
        QueryWrapper<Venue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sn", sn);
        if(venueService.getOne(queryWrapper) != null) {
            return ServerResponseEntity.fail("V10001","该场地编号已存在");
        }

        boolean result = venueService.addVenue(venueDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("V10001","添加场地信息失败");
    }

    /**
     * 修改
     * @param venueDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody VenueDto venueDto) {
        boolean result = venueService.updateVenue(venueDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("V10002","更新场地信息失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = venueService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除场地信息失败");
    }

    /**
     * 获取场地表单
     */
    @GetMapping("/getVenueForm")
    public ServerResponseEntity<VenueVO> getVenueForm(Long id) {
        Venue result = venueService.getById(id);
        return ServerResponseEntity.success(venueConvertor.entity2Vo(result));
    }
}
